/*
 * @Author: Dorange.Wang
 * @Date: 2021-07-07 15:44:56
 * @LastEditors: your name
 * @LastEditTime: 2021-07-14 18:32:46
 * @Description: file content
 */
/**
 * list转tree
 * 默认父节点parentId，子节点children，根节点0
 * @param {*} list
 * @param {*} parentId
 * @returns
 */
export function arrayToTree(list, parentId = 0, parentIds = []) {
  return list
    .filter((item) => item.parentId === parentId)
    .map((item, index, arr) => {
      const pids = parentId ? [...parentIds, parentId] : [...parentIds];
      return {
        ...item,
        _isFirst: index === 0,
        _isLast: index === arr.length - 1,
        _parentIds: pids,
        children: arrayToTree(list, item.id, pids),
      };
    });
}

export function getThousandsValue(value) {
  if (value === undefined || value === null) {
    return value;
  }
  let string = String(value);
  const isNegative = string.startsWith("-");
  const array = string.substr(isNegative ? 1 : 0).split(".");
  array[0] = String(array[0]).replace(/\d{1,3}(?=(\d{3})+$)/g, "$&,");
  return (isNegative ? "-" : "") + array.join(".");
}
